"""
增速状态：增速 1 转向下-2，减速-1，减速转头 2
"""
class ror_state:
    def __init__(self,timeperiod=1):
        self.timeperiod=timeperiod+1
        self.data=[]
        self.fx=None
        self.timekey=None
        self.his=[]
    def oncalc(self,d,timekey=None):
        if d is not None:
            if timekey:
                if timekey == self.timekey:
                    self.data[-1]=d
                    self.his.pop()
                    self.fx = self.his[-1]
                else:
                    self.data.append(d)
            else:
                self.data.append(d)
            if len(self.data)>self.timeperiod:
                self.data.pop(0)
            if len(self.data)>=self.timeperiod:
                if self.fx is not None:
                    if self.fx>=1:
                        if self.data[-1]<self.data[0]:
                            self.fx=-2
                        else:
                            self.fx=1
                    elif self.fx<=-1:
                        if self.data[-1]>self.data[0]:
                            self.fx=2
                        else:
                            self.fx=-1
                else:
                    if self.data[-1] > self.data[0]:
                        self.fx = 1
                    elif self.data[-1] < self.data[0]:
                        self.fx = -1
            if timekey   is not None:
                self.timekey = timekey
                self.his.append(self.fx)
                while len(self.his) > 2:
                    self.his.pop(0)
        return self.fx


if __name__ == '__main__':
    from jili.core import load, save
    from research.calcor.calcors_graph import graph_calcor
    k1m = load(r"D:\data\future_k1m_tq\TA005\TA005_20200102.pkl")
    roc_stat0=ror_state()
    cc = [
          {'calc_cmd': 'function', 'function': roc_stat0.oncalc, 'out': ['roc_stat0'], 'input': ["close","timekey"]}]
    c = graph_calcor(cc)
    k1ms = k1m[:90]
    for t in k1ms:
        t = c.onbar(t)
    print(t)
    t1 = k1m[90]
    t1["timekey"] = t["timekey"]
    t1 = c.onbar(t1)
    print(t1)
    print("end")
    print("end")